<template>
  <div>
    <el-button type="primary" @click="handleCreate"> 新建 </el-button>

    <el-table
      ref="table"
      class="mt10"
      :data="tableData"
      v-loading="loading"
      style="width: 100%"
      current-row-key="id"
      stripe
      border
      highlight-current-row
    >
      <el-table-column prop="key" label="模版KEy" />
      <el-table-column prop="name" label="模版名称" />
      <el-table-column prop="type" label="类型">
        <template slot-scope="scope">
          {{ scope.row.type === "EXPORT" ? "导出" : "导入" }}
        </template>
      </el-table-column>
      <el-table-column prop="createBy" label="创建人" />
      <el-table-column prop="createDate" label="创建时间">
        <template slot-scope="scope">
          {{ scope.row.createDate | moment }}
        </template>
      </el-table-column>
      <el-table-column prop="modifyBy" label="更新人" />
      <el-table-column prop="modifyDate" label="更新时间">
        <template slot-scope="scope">
          {{ scope.row.modifyDate | moment }}
        </template>
      </el-table-column>
      <el-table-column prop="actions" label="操作" width="180">
        <template slot-scope="scope">
          <span class="table-actionItem">
            <el-button type="text" @click="handleDesign(scope.row)"
              >设计</el-button
            >
          </span>
          <span class="table-actionItem">
            <el-button type="text" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
          </span>

          <span class="table-actionItem">
            <el-popconfirm
              title="确认要删除该模版，删除后无法恢复？"
              placement="top"
              @confirm="handleDelete(scope.row)"
            >
              <el-button class="link-danger" type="text" slot="reference"
                >删除</el-button
              >
            </el-popconfirm>
          </span>
        </template>
      </el-table-column>
    </el-table>
    <DateImportExportForm
      ref="DateImportExportForm"
      :modelInfo="modelInfo"
      @ok="loadData"
    />
  </div>
</template>

<script>
import {
  getHmexceltemplateList,
  deleteHmexceltemplateByModelIdByKey,
} from "@/apis/paas/HmExcelTemplateController.js";
import DateImportExportForm from "./DateImportExportForm";

export default {
  name: "RuleList",
  components: {
    DateImportExportForm,
  },

  props: {
    modelInfo: {
      type: Object,
      default: () => {},
    },
  },

  data() {
    return {
      loading: false,
      tableData: [],
    };
  },

  methods: {
    init() {
      this.loadData();
    },
    loadData() {
      this.loading = true;
      getHmexceltemplateList({
        modelCode: this.modelInfo.code,
      })
        .then((res) => {
          this.tableData = res;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    handleCreate() {
      this.$refs.DateImportExportForm.addForm();
    },
    //设计
    handleDesign(record) {
      const { origin, pathname } = window.location;
      const { key, name } = record;
      window.open(
        `${origin}${pathname}#/designExcelTemplate?modelCode=${this.modelInfo.code}&key=${key}&modelName=${this.modelInfo.name}`
      );
    },
    //编辑
    handleEdit(record) {
      this.$refs.DateImportExportForm.editForm({
        ...record,
      });
    },
    //删除
    handleDelete(record) {
      deleteHmexceltemplateByModelIdByKey({
        modelId: this.modelInfo.id,
        key: record.key,
      }).then((res) => {
        this.loadData();
        this.$message.success("操作成功");
      });
    },
  },
};
</script>

<style lang="scss" scoped></style>
